#include "include/gitinit.h"
#include "ui_gitinit.h"

GitInit::GitInit(QString directory,bool is_branch,QWidget *parent) :
    QDialog(parent),
    ui(new Ui::GitInit)
{
    ui->setupUi(this);
    this->directory=directory;
    this->is_branch=is_branch;
    if(is_branch){
        setWindowTitle("分支名填写");
        ui->label->setText("分支名");
        ui->lineEdit->setPlaceholderText("请输入分支名");
    }else{
        setWindowTitle("git仓库初始化");
    }
    process = new QProcess();
}

GitInit::GitInit(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::GitInit)
{
    ui->setupUi(this);
    setWindowTitle("git仓库初始化");
}

GitInit::~GitInit()
{
    this->reject();
    delete ui;
}

void GitInit::on_buttonBox_accepted()
{
    if(ui->lineEdit->text()==""){QMessageBox::critical(this, "错误", is_branch?"请输入分支名":"请输入仓库URL", QMessageBox::Ok);return;}
    if(directory==""){
        emit ToUrl(ui->lineEdit->text());
        return;
    }
    process->setWorkingDirectory(directory);
    /*1*/
    process->start("git init");
    process->waitForFinished();
    /*2*/
    process->start(tr("git remote add origin %1").arg(ui->lineEdit->text()));
    process->waitForFinished();
    QMessageBox::about(this, "git仓库初始化", "仓库初始化成功！");

}
